package com.android.fragmentdemo.util
import android.animation.Animator
import android.animation.ObjectAnimator
import android.animation.ValueAnimator
import android.animation.ValueAnimator.AnimatorUpdateListener
import android.content.Context
import android.graphics.PixelFormat
import android.os.Handler
import android.os.Looper
import android.view.Gravity
import android.view.LayoutInflater
import android.view.WindowManager
import android.view.animation.Animation
import android.view.animation.Animation.AnimationListener
import androidx.databinding.DataBindingUtil
import com.android.fragmentdemo.R
import com.android.fragmentdemo.databinding.LayoutWindowmanagerContentBinding
import java.lang.ref.WeakReference

object WindowManagerUtil {

    private lateinit var weakReference: WeakReference<Context>
    private lateinit var mHandler: Handler

    fun init(context: Context) {
        weakReference = WeakReference(context)
        mHandler = Handler(Looper.getMainLooper())
    }

    fun showContentView(content: String) {
        weakReference.get()?.let {
            val mBinding: LayoutWindowmanagerContentBinding = DataBindingUtil.inflate(
                LayoutInflater.from(it),
                R.layout.layout_windowmanager_content, null, false
            )
            val windowManager: WindowManager =
                it.getSystemService(Context.WINDOW_SERVICE) as WindowManager
            val layoutParams = WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                it.resources.getDimension(R.dimen.dp_60).toInt(),
                WindowManager.LayoutParams.TYPE_APPLICATION,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSPARENT
            )

            layoutParams.gravity = Gravity.TOP or Gravity.CENTER_HORIZONTAL
            //layoutParams.gravity = Gravity.CENTER or Gravity.CENTER_HORIZONTAL
            //layoutParams.gravity = Gravity.BOTTOM or Gravity.CENTER_HORIZONTAL

            //layoutParams.x = context.resources.getDimension(R.dimen.dp_10).toInt()
            layoutParams.y = it.resources.getDimension(R.dimen.dp_10).toInt()

            mBinding.tvWindowManagerContent.text = content
            windowManager.addView(mBinding.root, layoutParams)

            mHandler.postDelayed({
                windowManager.removeView(mBinding.root)
            },3000L)

            //动画形式
            /*ObjectAnimator.ofFloat(mBinding.root,"alpha",1F,0F).apply {
                duration = 3000
                addListener(object : Animator.AnimatorListener {
                    override fun onAnimationStart(animation: Animator) {}
                    override fun onAnimationEnd(animation: Animator) {
                        windowManager.removeView(mBinding.root)
                    }
                    override fun onAnimationCancel(animation: Animator) {}
                    override fun onAnimationRepeat(animation: Animator) {}
                })
                start()
            }*/
        }
    }
}